package cn.edu.cug.cs.gtl.plugins.h3dfile.layer;

import cn.edu.cug.cs.gtl.api.wrapper.TableWrapper;
import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.cga.voxelization.VoxelModeller;
import cn.edu.cug.cs.gtl.io.FileDataSplitter;
import cn.edu.cug.cs.gtl.protos.*;
import com.google.protobuf.Any;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Locale;
import java.util.ResourceBundle;

import static cn.edu.cug.cs.gtl.protos.GeometryType.POINT3D;

public class VoxelizationToFileAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        ResourceBundle resourceBundle = ResourceBundle.getBundle("h3dfile_resource", Locale.getDefault());

        name=resourceBundle.getString("popupmenu.model3d.layer.voxelization");
        menuItem.setText(name);


        this.theApp = theApp;
        menuItem.addActionListener(this);
        JPopupMenu popupMenu = theApp.getMainFrame()
                .getProjectPanel()
                .getTreeComponent()
                .getLayerNodePopupMenu();
        popupMenu.addSeparator();
        popupMenu.add(menuItem);
        popupMenu.addSeparator();
    }

    /**
     * Invoked when an action occurs.
     *
     * @param e the event to be processed
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        final JFileChooser ivFileChooser = new JFileChooser(
                new File(System.getProperty("user.dir")) );
        ivFileChooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File f) {
                if ( f == null )
                    return false;
                if ( f.isDirectory() )
                    return true;
                return f.getName().toLowerCase().endsWith(".vox");
            }

            @Override
            public String getDescription() {
                return "vox File";
            }
        });

        ivFileChooser.removeChoosableFileFilter(ivFileChooser.getAcceptAllFileFilter());

        if ( ivFileChooser.showSaveDialog(null) != JFileChooser.APPROVE_OPTION )
            return;

        String fileName = ivFileChooser.getSelectedFile().getAbsolutePath();
        try {
            Layer layer = theApp.getCurrentLayer();
            if(layer!=null) {
                VoxelModeller vox = new VoxelModeller();
                //VoxelModel voxelModel = vox.voxelize(layer);
            }
        }
        catch (Exception exception){
            exception.printStackTrace();
        }
    }
}
